﻿using MathNet.Numerics.Providers.LinearAlgebra;
using MathNet.Numerics.Random;
using MathNet.Numerics.Statistics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace phminarm.algorithm.FeatureExtraction
{
    public class TimeDomainFeatures
    {
        public TimeDomainFeatures(float[] timewave)
        {
            this.timewave = timewave;
        }
        public float[] timewave { get; private set; }
        //最大值F0
        public float max { get; private set; }
        //最小值F1
        public float min { get; private set; }
        //峰峰值F2
        public float pp { get; private set; }
        //平均值F3
        public float mean { get; private set; }
        //平均幅值F4
        public float meanabs { get; private set; }
        //均方根值F5
        public float rms { get; private set; }
        //方根幅值F6
        public float smr { get; private set; }
        //方差F7
        public float varince { get; private set; }
        //标准差F8
        public float standard { get; private set; }
        //偏度F9
        public float skewness { get; private set; }
        //峭度F10
        public float kurtosis { get; private set; }
        //偏度因子F11
        public float skewnessf { get; private set; }
        //峭度因子F12
        public float kurtosisf { get; private set; }
        //峰值因子F13
        public float peakf { get; private set; }
        //脉冲因子F14
        public float pulsef { get; private set; }
        //波形因子F15
        public float formf { get; private set; }
        //裕度因子F16
        public float clearancef { get; private set; }
        //18.提取全部时域特征
        public void GetAllTimeDomainFeatures()
        {
            int N = timewave.Length;
            //最大值F0
            min = timewave.Min();
            //最小值F1
            max = timewave.Max();
            //峰峰值F2
            pp = max - min;
            //平均值F3
            mean = Convert.ToSingle(timewave.Average());
            //平均幅值F4
            meanabs = timewave.Select(x => Math.Abs(x)).Average();
            //均方根值F5
            rms = Convert.ToSingle(timewave.RootMeanSquare());
            //方根幅值F6
            smr = Convert.ToSingle(Math.Pow(timewave.Select(x => Math.Sqrt(Math.Abs(x))).Average(), 2));
            //方差F7
            varince = Convert.ToSingle(timewave.Variance());
            //标准差F8
            standard = Convert.ToSingle(timewave.StandardDeviation());
            //偏度F9
            skewness = Convert.ToSingle(timewave.Sum(x => Math.Pow((x - mean), 3)) / (N - 1));
            //峭度F10
            kurtosis = Convert.ToSingle(timewave.Sum(x => Math.Pow((x - mean), 4)) / (N - 1));
            //偏度因子F11
            skewnessf = Convert.ToSingle(skewness / Math.Pow(standard, 3));
            //峭度因子F12
            kurtosisf = Convert.ToSingle(kurtosis / Math.Pow(standard, 4));
            //峰值因子F13
            peakf = max / rms;
            //脉冲因子F14
            pulsef = max / meanabs;
            //波形因子F15
            formf = rms / meanabs;
            //裕度因子F16
            clearancef = max / smr;
        }
    }
}
